package com.ars.flight;

import com.ars.db.DBTools;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import javax.swing.*;

public class AddPlaneController {
    @FXML
    private TextField tfPlaneName;
    @FXML
    private TextField tfHeadSeats;
    @FXML
    private TextField tfBusinessSeats;
    @FXML
    private TextField tfEconomySeats;
    @FXML
    private TextField tfMaxLength;

    //更新数据库所需
    private String planeName;
    private int firstSeats;
    private int businessSeats;
    private int economySeats;
    private int maxLength;

    @FXML
    public void addButtonAction() {
        if(this.getFormValue()) {
            this.updateDB();
            JOptionPane.showMessageDialog(null,"操作成功！");
        }else{
            JOptionPane.showMessageDialog(null,"请将表单填写完整！");
        }
    }

    @FXML
    public void cancelButtonAction() {
        tfBusinessSeats.clear();
        tfEconomySeats.clear();
        tfHeadSeats.clear();
        tfMaxLength.clear();
        tfPlaneName.clear();
    }

    //获取表单数据
    private boolean getFormValue() {
        planeName = tfPlaneName.getText();
        try {
            firstSeats = Integer.parseInt(tfHeadSeats.getText());
            businessSeats = Integer.parseInt(tfBusinessSeats.getText());
            economySeats = Integer.parseInt(tfEconomySeats.getText());
            maxLength = Integer.parseInt(tfMaxLength.getText());
        }catch (Exception e){
            return false;
        }

        return !(planeName.isEmpty());
    }

    //更新数据库
    private void updateDB() {
        String sql = "INSERT INTO plane VALUES(NULL,'" + planeName + "'," + firstSeats + "," + businessSeats + "," + economySeats + "," + maxLength + ");";
        new DBTools().update(sql);
    }
}
